<?php
/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
class news_model extends backend_model {

    function __construct()
    {
        parent::__construct('news','news');
    }
    function get($news_id){
        $query=$this->db
                ->where("news_id",$news_id)
                ->or_where("news_alias",$news_id)
                ->get("news");
        return $query->row();
    }
    function get_in_cate_ids($cate_ids=null){
        if($cate_ids!=null){
            if(!is_array($cate_ids))$cate_ids=array($cate_ids);
            $this->db->where_in('news_category',$cate_ids);
        }
        $query=$this->db
                ->order_by('news_insert','DESC')
                ->get("news"); 
        return $query->result();
    }
    function get_by_cate_type($type=null){
        if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
                ->from('news')
                ->join('categories','cate_id=news_category')
                ->order_by('news_insert','DESC')
                ->get(); 
        return $query->result();
    }
    function get_in_cate_type($types=null){
        if($types){
            if(!is_array($types))$types=array($types);
            $this->db->where('cate_type',$types);
        }
        $query=$this->db
                ->from('news')
                ->join('categories','cate_id=news_category')
                ->order_by('news_insert','DESC')
                ->get(); 
        return $query->result();
    }
    
    function binding($type=null){
        //$position=isset($_SESSION["auth"]["user"])?$_SESSION["auth"]["user"]->ause_position:0;
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS news_id as _id,news_title,news_alias,news_category,
                news_thumb,news_status,news_insert,news_update,news_delete,`cate_title`
            FROM `news`
                JOIN `categories` ON (`cate_id` = `news_category`)
            ";
        $strWhere="
            WHERE  true
            ";
        if($type){$type=  mysql_real_escape_string($type);$strWhere.=" AND `cate_type` = '$type'";}
//        if($_SESSION["ADP"][CCTRL]["display"]===0){
//            $strWhere.=" AND `news_delete` IS NULL";
//        }elseif($_SESSION["ADP"][CCTRL]["display"]===-1){
//            $strWhere.=" AND `news_delete` IS NOT NULL";
//        }
        $strOrderBy="ORDER BY `news_insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function colmod(){
        return array(
            array(  
                "text"          =>"ID"            
                ,"datafield"    =>"_id" 
                ,"cellsalign"   =>"right"
                ,"width"        => 80 
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Title"            
                ,"datafield"    =>"news_title" 
                //,"width"        => 220
                ,"editable"     =>false
                ),
            array(  
                "text"          =>"Alias"            
                ,"datafield"    =>"news_alias" 
                //,"width"        => 220
                ,"editable"     =>false
                //,"hidden"       =>true
                ),
//            array(  
//                "text"          =>"Category"            
//                ,"datafield"    =>"cate_title" 
//                //,"width"        => 220
//                ,"editable"     =>false
//                ,"hidden"       =>true
//                ),
            array(  
                "text"          =>"Category"            
                ,"datafield"    =>"cate_title" 
                ,"columntype"   =>"dropdownlist"
                ,"filtertype"   =>"checkedlist"  
                //,"filteritems"  => array("Admin","Administrator","User","Customer","Developer","Partner")
                ,"width"        => 120 
                ,"editable"     =>false
                ),
            array(  
                "text"          =>"Active"            
                ,"datafield"    =>"news_status" 
                ,"type"         =>"bool"
                ,"width"        => 100   
                ,"columntype"   =>"checkbox"
                ,"threestatecheckbox"=> false
                ,"filtertype"   =>"bool"  
                ,"editable"     =>true
                ,"filterable"   =>false
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Insert"            
                ,"datafield"    =>"news_insert" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Update"            
                ,"datafield"    =>"news_update" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Delete"            
                ,"datafield"    =>"news_delete" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                )
        );
    }
}
?>
